//
//  HomeAddMenuView.m
//  CarOfHome2
//
//  Created by dlios on 15/9/23.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "HomeAddMenuView.h"
#import "STYHomeBtnModel.h"
#import "STYAddMenuCell.h"
#import "HeaderReusableView.h"
@interface HomeAddMenuView ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@end

@implementation HomeAddMenuView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
        self.flowLayout.itemSize = CGSizeMake(self.frame.size.width / 5, SAHeight * 30);
        self.flowLayout.minimumInteritemSpacing = 0;
        self.flowLayout.minimumLineSpacing = 10 ;
        self.flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SAHeight * 375, SAHeight * 533) collectionViewLayout:self.flowLayout];
        self.collectionView.backgroundColor = [UIColor whiteColor];
        
        self.collectionView.dataSource = self;
        self.collectionView.delegate = self;
        
        [self.collectionView registerClass:[STYAddMenuCell class] forCellWithReuseIdentifier:@"reuse"];
        [self.collectionView registerClass:[HeaderReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
        [self addSubview:self.collectionView];
        self.collectionView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"menuGround"]];
        
        
        
    }
    return self;
}

- (void)setOldMenuArr:(NSMutableArray *)oldMenuArr
{
    if (_oldMenuArr != oldMenuArr) {
        _oldMenuArr = oldMenuArr;
    }
    
    [self.collectionView reloadData];
}

#pragma mark - 协议
#pragma mark -
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.menuArr.count;
    }
    else
    {
        return self.oldMenuArr.count;
    }
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    STYAddMenuCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        if (indexPath.item < 3) {
            cell.contentView.backgroundColor = [UIColor grayColor];
        }else{
            cell.contentView.backgroundColor = [UIColor whiteColor];
            //            cell.alpha = 0.5;
        }
        cell.menuLable.text = [[self.menuArr objectAtIndex:indexPath.item] btnName];
        return cell;
    }else{
        cell.contentView.backgroundColor = [UIColor whiteColor];
        cell.menuLable.text = [[self.oldMenuArr objectAtIndex:indexPath.item] btnName];
        return cell;
    }
    
    
}



#pragma mark -

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        HeaderReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
        if (indexPath.section == 0) {
            headerView.headerLable.text = @"已经订阅";
            NSLog(@"%f", headerView.headerLable.frame.origin.x);
            NSLog(@"%f", headerView.headerLable.frame.origin.y);
            return headerView;
        }else if(indexPath.section == 1){
            headerView.headerLable.text = @"没有订阅";
            return headerView;
        }else{
            return nil;
        }
    }else{
        return nil;
    }
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(SAWindth * 375, SAWindth * 30);
}

//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && indexPath.item < 3) {
        return NO;
    }
    else
        return YES;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        STYHomeBtnModel *model = [self.menuArr objectAtIndex:indexPath.item];
        
        [self.menuArr removeObjectAtIndex:indexPath.item];
        [self.oldMenuArr addObject:model];

        [self.collectionView reloadData];
    }else{
        STYHomeBtnModel *model = [self.oldMenuArr objectAtIndex:indexPath.item];
        
        [self.oldMenuArr removeObjectAtIndex:indexPath.item];
        [self.menuArr addObject:model];
        [self.collectionView reloadData];
    }
    

    
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
